package com.rongshu.view.home.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.discovery.bean.AuthorDetailBean;
import com.rongshu.view.discovery.bean.SearchAuthorResponse;
import com.rongshu.view.discovery.bean.SearchBody;
import com.rongshu.view.home.contract.SearchAtView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Func1;

public class SearchAtPresenter extends BasePresenter<SearchAtView> {

    public SearchAtPresenter(Context context) {
        super(context);
    }

    public void getSearchAuthor(String msg, int pageIndex, int pageSize, int user_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        SearchBody body = new SearchBody();
        body.setSearchWhere(msg);
        RetorfitService.searchAuthor(user_id, pageIndex, pageSize,
                Common.encryptionSign(map), body)
                .map(new Func1<SearchAuthorResponse, List<AuthorDetailBean>>() {
                    @Override
                    public List<AuthorDetailBean> call(SearchAuthorResponse response) {
                        return response.getResponse();
                    }
                })
                .map(new Func1<List<AuthorDetailBean>, List<AuthorDetailBean>>() {
                    @Override
                    public List<AuthorDetailBean> call(List<AuthorDetailBean> authorDetailBeanList) {
                        for (AuthorDetailBean bean : authorDetailBeanList) {
                            bean.setUser_id_to(bean.getUserid());
                        }
                        return authorDetailBeanList;
                    }
                })
                .subscribe(new Subscriber<List<AuthorDetailBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<AuthorDetailBean> list) {
                        if (getView() != null) {
                            getView().onSearchAtUser(list);
                        }
                    }
                });
    }

    /**
     * 获取我的主页 我的关注 1，我的粉丝 2 ,我的黑名单 3
     */
    public void getSetUserRelationships(int user_id, int type, int page, int pageSize, String sign) {
        RetorfitService.getSetUserRelationships(user_id, type, page, pageSize, sign)
                .map(new Func1<SearchAuthorResponse, List<AuthorDetailBean>>() {
                    @Override
                    public List<AuthorDetailBean> call(SearchAuthorResponse response) {
                        return response.getResponse();
                    }
                })
                .subscribe(new Subscriber<List<AuthorDetailBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<AuthorDetailBean> list) {
                        if (getView() != null) {
                            getView().onSearchAtUser(list);
                        }
                    }
                });
    }
}
